//
//  clsOtherFun.h
//  AntsExpress
//
//  Created by slccper on 16/1/1.
//  Copyright © 2016年 slccper. All rights reserved.
//

#import "clsOtherFun.h"
#import "clsFileFun.h"
#import "AppDelegate.h"
#import <CommonCrypto/CommonCrypto.h>
#import <objc/runtime.h>
#import "LoadingView.h"
#import "MBProgressHUD.h"
#import "clsUser.h"
@implementation clsOtherFun

static NSString *system_weburl = @"weburl";

+ (float)MathMoney:(float)lon :(float)width :(float)height :(NSInteger)day
{
    if (day>0) {
        float tiji = lon *width*height;
        NSMutableArray *tempMath = [NSMutableArray array];
        for (int i = 0; i<11; i++) {
            int num = [[NSString stringWithFormat:@"%d8",i]intValue];
            [tempMath addObject:[NSNumber numberWithInt:(num *num *num)]];
        }
        float money = 0.0;
        for (int i=1; i<10; i++) {
            if (tiji>=[tempMath[i] intValue] &&tiji <[tempMath[i+1] intValue]) {
                money = 2+0.5 *i *(i-1);
            }else if (tiji <[tempMath[1]intValue])
            {
                money=2;
            }
        }
        return money *day ;
    }else{
        return 0;
    }
}
+ (void)setPwd:(NSString *)pwd
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:pwd forKey:@"loginpwd"];
    [defaults synchronize];
}
+ (void)setName:(NSString *)name
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:name forKey:@"loginname"];
    [defaults synchronize];
}
+ (void)setReName:(NSString *)name
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:name forKey:@"loginname1"];
    [defaults synchronize];
}
+ (void)setNew:(NSString *)name
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:name forKey:@"New"];
    [defaults synchronize];

}
+ (NSString *)getNew
{
    NSString *name = @"";
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"New"]) {
        name = [defaults objectForKey:@"New"];
    }
    return name;
}
+ (NSString *)getReName
{
    NSString *name = @"";
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"loginname1"]) {
        name = [defaults objectForKey:@"loginname1"];
    }
    return name;
}
+ (NSString *)getName
{
    NSString *name = @"";
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"loginname"]) {
        name = [defaults objectForKey:@"loginname"];
    }
    return name;
}
+ (NSString *)getPwd
{
    NSString *password = @"";
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"loginpwd"]) {
        password = [defaults objectForKey:@"loginpwd"];
    }
    return password;
}

+ (NSString *)getIsFirstRun {
    NSString *key = @"IsFirstRun";
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults objectForKey:key];
}

+ (void)setIsFirstRun {
    NSString *key = @"IsFirstRun";
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:@"1" forKey:key];
    [defaults synchronize];
}
+ (void)setNewTitle:(NSString *)Title
{
    NSString *key = @"NewTitle";
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:Title forKey:key];
    [defaults synchronize];
}
+ (NSString *)getNewTitle
{
    NSString *NewTitle = @"";
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"NewTitle"]) {
        NewTitle = [defaults objectForKey:@"NewTitle"];
    }
    return NewTitle;
}
+(NSString *)getHttpBody:(NSDictionary *)message
{
    NSString *result = @"";
    for(int i=0;i<message.allKeys.count;i++){
        result = [NSString stringWithFormat:@"%@%@=%@&",result,message.allKeys[i],message.allValues[i]];
    }
    if (result.length>1) {
        NSRange range ={0,result.length-1};
        result = [result substringWithRange:range];
    }
    return result;
}
+(NSString*)getAntsURL {
    NSString *str=[NSString stringWithFormat:@"http://ot-express-macau.com:8100/test.asmx"];
	return str;
}
+(NSString *)getWSUrl:(NSString *)url
{
    NSString *str=[clsOtherFun getAntsURL];
    if (![url isEqualToString:@""]) {
        str = [NSString stringWithFormat:@"%@/%@",str,url];
    }
    return str;
}

+(NSString*)getConfigValue:(NSString*)key {  
	NSString *path = [[NSBundle mainBundle] pathForResource:@"config" ofType:@"plist"];
	NSMutableDictionary *dict=[[NSMutableDictionary alloc] initWithContentsOfFile:path];  
	
	NSString *object=[dict objectForKey:key];  
	if (object==nil || object.length==0) {  
		object=@"";  
	}  
	
	return object;  
}

+(void)setDeviceToken:(NSString *)token
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:token forKey:@"token"];
    [defaults synchronize];
}
+(NSString *)getDeviceToken{
    NSString *token=@"";
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"token"]) {
        token = [defaults objectForKey:@"token"];
    }
    return token;
    
}


+(NSString *)PhoneNumberEntityEncode:(NSString *)pstrPhoneNumber {
	NSString *str=[pstrPhoneNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
	str=[str stringByReplacingOccurrencesOfString:@")" withString:@""];
	str=[str stringByReplacingOccurrencesOfString:@"-" withString:@""];
	str=[str stringByReplacingOccurrencesOfString:@" " withString:@""];
	return str;
}

+(void)ShowNoNetworkMsg {
	[self showMessage:@"無法連接網絡，請稍後重試" withImage:nil andTime:1.5];

}

+(void)ShowMsgWithNoData {
   
	[self showMessage:@"無法獲取數據，請稍後重試" withImage:nil andTime:1.5];
}
/**
 显示HUD
 */
+ (void) showMessage:(NSString *)message withImage:(UIImage *)image andTime:(float) time{
    
    MBProgressHUD *HUD = [[MBProgressHUD alloc]  initWithWindow:[UIApplication sharedApplication].keyWindow];
    [[UIApplication sharedApplication].keyWindow addSubview:HUD];
    HUD.customView = [[UIImageView alloc] initWithImage:image];
    
    // Set custom view mode
    HUD.mode = MBProgressHUDModeText;
    HUD.margin=12.0;
    HUD.labelText =message;
    HUD.labelFont=[UIFont systemFontOfSize:14];
    
    [HUD show:YES];
    [HUD hide:YES afterDelay:time];
    
}

+(BOOL)CheckNetworkStatus:(BOOL) isShowMsg {
	AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
	NetworkStatus status = [appDelegate.internetReach currentReachabilityStatus];
	appDelegate=nil;
	
	if (status == NotReachable) {
		if (isShowMsg) {
			[self ShowNoNetworkMsg];
		}
		return NO;
	}
	return YES;
}
+(SEL) selectorForColumnName: (NSString *)pcolumn {
    if([pcolumn isEqualToString:@"id"]) pcolumn=[pcolumn uppercaseString];
    pcolumn=[pcolumn stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[pcolumn substringWithRange:NSMakeRange(0,1)] uppercaseString]];
    return NSSelectorFromString([NSString stringWithFormat: @"set%@:",  pcolumn]);
}

+(void)ShowMsg:(NSString *)msg
{
    UIAlertView * alter=[[UIAlertView alloc] initWithTitle:@"" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"確定", nil];
    [alter show];
}

static LoadingView *_newLoadingView = nil;

+ (void)showLoadingView:(NSString *)title {
    if (!_newLoadingView) {
        _newLoadingView = [[LoadingView alloc] init];
        [_newLoadingView startLoading:title];
    } else {
        [_newLoadingView setTitle:title];
    }
}

+ (void)hideLoadingView {
    if (_newLoadingView) {
        [_newLoadingView stopLoading];
        _newLoadingView = nil;
    }
}
+(NSString *)getencodeName{
    NSString *strencodeName=@"";
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"encodeName"]) {
        strencodeName = [defaults objectForKey:@"encodeName"];
    }
    return strencodeName;
}
+(void)setencodeName:(NSString *)encodeName
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    encodeName = [encodeName stringByReplacingOccurrencesOfString:@"+" withString:@" "];
    [defaults setObject:encodeName forKey:@"encodeName"];
    [defaults synchronize];
}
+(NSString *)GetMoney:(NSString *)h :(NSString *)w :(NSString *)l :(NSString *)day
{
    return @"";
}
+(NSString *)getUserID{
    NSString *strUserID=@"";
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"userid"]) {
        strUserID = [defaults objectForKey:@"userid"];
    }
    return strUserID;
}
+(void)setUserID:(NSString *)userid
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:userid forKey:@"userid"];
    [defaults synchronize];
}
+(void)setUser:(clsUser *)user
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:user.MemberId forKey:@"MemberId"];
    [defaults setObject:user.MemberName forKey:@"MemberName"];
    [defaults setObject:user.MemberPhoto forKey:@"MemberPhoto"];
    [defaults setObject:user.RealName forKey:@"RealName"];
    [defaults setObject:user.MemberSex forKey:@"MemberSex"];
    [defaults setObject:user.Birthday forKey:@"Birthday"];
    [defaults setObject:user.qq forKey:@"qq"];
    [defaults setObject:user.wechart forKey:@"wechart"];
    [defaults setObject:user.MemberPhone forKey:@"MemberPhone"];
    [defaults setObject:user.Email forKey:@"Email"];
    [defaults setObject:user.areaRemark forKey:@"areaRemark"];
    [defaults setObject:user.Address forKey:@"Address"];
    [defaults setObject:user.ResivePwd forKey:@"ResivePwd"];
    [defaults setObject:user.VIPMoney forKey:@"VIPMoney"];
    [defaults synchronize];
}
+(NSString *)getMemberId
{
    NSString *MemberId=@"";
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"MemberId"]) {
        MemberId = [defaults objectForKey:@"MemberId"];
    }
    return MemberId;
}
+(void)setVIPMoney:(NSString *)Money
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:Money forKey:@"VIPMoney"];
    [defaults synchronize];
}
+(NSString *)getVIPMoney
{
    NSString *VIPMoney=@"";
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"VIPMoney"]) {
        VIPMoney = [defaults objectForKey:@"VIPMoney"];
    }
    return VIPMoney;
}
+(NSString *)getMemberName
{
    NSString *MemberName=@"";
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"MemberName"]) {
        MemberName = [defaults objectForKey:@"MemberName"];
    }
    return MemberName;
}
+(NSString *)getMemberPhoto
{
    NSString *MemberPhoto=@"";
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"MemberPhoto"]) {
        MemberPhoto = [defaults objectForKey:@"MemberPhoto"];
    }
    return MemberPhoto;
}
+(NSString *)getareaRemark
{
    NSString *areaRemark=@"";
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"areaRemark"]) {
        areaRemark = [defaults objectForKey:@"areaRemark"];
    }
    return areaRemark;
}
+(NSString *)getRealName
{
    NSString *RealName=@"";
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"RealName"]) {
        RealName = [defaults objectForKey:@"RealName"];
    }
    return RealName;
}
+(NSString *)getMemberSex
{
    NSString *MemberSex=@"";
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"MemberSex"]) {
        MemberSex = [defaults objectForKey:@"MemberSex"];
    }
    return MemberSex;
}
+(NSString *)getBirthday
{
    NSString *Birthday=@"";
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"Birthday"]) {
        Birthday = [defaults objectForKey:@"Birthday"];
    }
    return Birthday;
}
+(NSString *)getQQ
{
    NSString *qq=@"";
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"qq"]) {
        qq = [defaults objectForKey:@"qq"];
    }
    return qq;
}

+(NSString *)getwechart
{
    NSString *wechart=@"";
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"wechart"]) {
        wechart = [defaults objectForKey:@"wechart"];
    }
    return wechart;
}
+(NSString *)getMemberPhone
{
    NSString *MemberPhone=@"";
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"MemberPhone"]) {
        MemberPhone = [defaults objectForKey:@"MemberPhone"];
    }
    return MemberPhone;
}
+(NSString *)getEmail
{
    NSString *Email=@"";
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"Email"]) {
        Email = [defaults objectForKey:@"Email"];
    }
    return Email;
}
+(NSString *)getAddress
{
    NSString *Address=@"";
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"Address"]) {
        Address = [defaults objectForKey:@"Address"];
    }
    return Address;
}
+(NSString *)getResivePwd
{
    NSString *ResivePwd=@"";
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"ResivePwd"]) {
        ResivePwd = [defaults objectForKey:@"ResivePwd"];
    }
    return ResivePwd;
}
+(void)setEmail:(NSString *)Email
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:Email forKey:@"Email"];
    [defaults synchronize];
}
+(void)setwechart:(NSString *)wechart
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:wechart forKey:@"wechart"];
    [defaults synchronize];
}
+(void)setqq:(NSString *)qq
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:qq forKey:@"qq"];
    [defaults synchronize];
}
+(void)setResivePwd:(NSString *)ResivePwd
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:ResivePwd forKey:@"ResivePwd"];
    [defaults synchronize];
}
+(void)setMemberPhoto:(NSString *)user
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:user forKey:@"MemberPhoto"];
    [defaults synchronize];
}
+(void)setMemberPhone:(NSString *)user
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:user forKey:@"MemberPhone"];
    [defaults synchronize];
}
+(void)setMemberSex:(NSString *)MemberSex
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:MemberSex forKey:@"MemberSex"];
    [defaults synchronize];
}
+(void)setAddress:(NSString *)user
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:user forKey:@"Address"];
    [defaults synchronize];
}
+(NSString*)base64forData:(NSData*)theData {
    
    const uint8_t* input = (const uint8_t*)[theData bytes];
    NSInteger length = [theData length];
    
    static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    
    NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
    uint8_t* output = (uint8_t*)data.mutableBytes;
    
    NSInteger i;
    for (i=0; i < length; i += 3) {
        NSInteger value = 0;
        NSInteger j;
        for (j = i; j < (i + 3); j++) {
            value <<= 8;
            
            if (j < length) {
                value |= (0xFF & input[j]);
            }
        }
        
        NSInteger theIndex = (i / 3) * 4;
        output[theIndex + 0] =                    table[(value >> 18) & 0x3F];
        output[theIndex + 1] =                    table[(value >> 12) & 0x3F];
        output[theIndex + 2] = (i + 1) < length ? table[(value >> 6)  & 0x3F] : '=';
        output[theIndex + 3] = (i + 2) < length ? table[(value >> 0)  & 0x3F] : '=';
    }
    
    return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
}
#pragma mark Hash
+ (NSString *)MD5WithString:(NSString *)password {
	const void *cStr = [password UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
	CC_MD5(cStr, strlen(cStr), result); // This is the md5 call
	return [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
			result[0], result[1], result[2], result[3],	result[4], result[5], result[6], result[7],
			result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15]];
}

+ (NSString *)HMACMD5WithString:(NSString *)password {
	const void *cStr = [password UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CCHmac(kCCHmacAlgMD5, cStr, strlen(cStr), cStr, strlen(cStr), result);
	//CC_MD5(cStr, strlen(cStr), result); // This is the md5 call
	return [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
			result[0], result[1], result[2], result[3],	result[4], result[5], result[6], result[7],
			result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15]];
}
+ (void) ObjectSetValue:(NSObject *)objinstance : (NSString *)propertyname  :(NSString *)value{
    const NSSet *boolTypes = [NSSet setWithObjects: @"Tc", nil];
    const NSSet *intTypes  = [NSSet setWithObjects: @"Ti", nil];
    const NSSet *floatTypes = [NSSet setWithObjects: @"Tf", nil];
    const NSSet *doubleTypes = [NSSet setWithObjects: @"Td", nil];
    const NSSet *stringTypes = [NSSet setWithObjects: @"T@\"NSString\"", nil];
    
    objc_property_t otype=class_getProperty([objinstance class], [propertyname UTF8String]);
    NSString *dataType = [NSString stringWithUTF8String: property_getAttributes(otype)];
    dataType=([dataType componentsSeparatedByString:@","])[0];
    
    if ([stringTypes containsObject: dataType]) {
        [objinstance setValue:value forKey:propertyname];
    }if ([boolTypes containsObject: dataType]) {
        BOOL blnvalue=[value isEqualToString:@"false"] ? NO : YES;
        [objinstance setValue: [NSNumber numberWithBool:blnvalue] forKey:propertyname];
    }else if ([intTypes containsObject: dataType]) {
        int intvalue=[value intValue];
        [objinstance setValue: [NSNumber numberWithInt:intvalue] forKey:propertyname];
    }else if ([floatTypes containsObject: dataType]) {
        float floatvalue=[value floatValue];
        [objinstance setValue: [NSNumber numberWithFloat:floatvalue] forKey:propertyname];
    }else if ([doubleTypes containsObject: dataType]) {
        double doublevalue=[value doubleValue];
        [objinstance setValue: [NSNumber numberWithDouble:doublevalue] forKey:propertyname];
    }else{
        [objinstance setValue:value forKey:propertyname];
    }
}

+ (UIImage*) createImageWithColor: (UIColor*) color
{
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}
+ (NSString *)DesWithString:(NSString *)str
{
    NSData *btsData = [str dataUsingEncoding:NSUTF8StringEncoding];
    NSUInteger len;
    Byte *byteData = (Byte*)malloc(len);
    memcpy(byteData, [btsData bytes], len);
    NSMutableData *bts2Data = [NSMutableData dataWithLength:len];
    NSData *date =[bts2Data initWithBytes:byteData length:len];
    NSString *desstr = [[NSString alloc]initWithData:date encoding:NSUTF8StringEncoding];
    return desstr;
}
+ (void)addAllLayoutConstraints:(UIView *)view
{
    NSDictionary *viewsDictionary = [clsOtherFun visualFormatStringViewsDictionaryWithSubViews:view.subviews];
    
    NSString *visualFormatString = [clsOtherFun visualFormatConstraintStringWithSubviews:view.subviews];
    
    [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:visualFormatString
                                                                 options:0
                                                                 metrics:nil
                                                                   views:viewsDictionary]];
    
    [view addConstraints:[clsOtherFun separatorWidthConstraintsWithSeparators:view.subviews]];
    [view addConstraints:[clsOtherFun centerAlignmentConstraintsWithButtons:view.subviews :view]];
}
+ (NSDictionary *)visualFormatStringViewsDictionaryWithSubViews:(NSArray *)subviews
{
    NSMutableDictionary *viewsDictionary = [[NSMutableDictionary alloc] init];
    
    for (UIView *subview in subviews) {
        NSString *key = [NSString stringWithFormat:@"view%ld", (long)subview.tag];
        viewsDictionary[key] = subview;
    }
    return viewsDictionary;
}
+ (NSString *)visualFormatConstraintStringWithSubviews:(NSArray *)subviews
{
    NSEnumerator *buttonsEnumerator = [subviews objectEnumerator];
    NSMutableArray *constraintParts = [[NSMutableArray alloc] init];
    
    UIView *view;
    while (view = [buttonsEnumerator nextObject]) {
        NSString *viewnFormat = [NSString stringWithFormat:@"view%ld", (long)view.tag];
        [constraintParts addObject:[NSString stringWithFormat:@"[%@]", viewnFormat]];
        view.translatesAutoresizingMaskIntoConstraints=NO;
        if ([view isEqual:[subviews lastObject]]) {
            break;
        }
    }
    NSMutableString *constraint = [NSMutableString stringWithFormat:@"H:|"];
    [constraint appendString:[constraintParts componentsJoinedByString:@""]];
    [constraint appendString:[NSString stringWithFormat:@"|"]];
    
    return constraint;
}
+ (NSArray *)separatorWidthConstraintsWithSeparators:(NSArray *)separators
{
    NSMutableArray *constraints = [[NSMutableArray alloc] init];
    
    [separators enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        UIView *separator = (UIView *)obj;
        UIView *targetSeparator;
        
        if ([obj isEqual:[separators lastObject]]) {
            targetSeparator = [separators firstObject];
        } else {
            targetSeparator = [separators objectAtIndex:(idx + 1)];
        }
        
        NSLayoutConstraint *constraint;
        constraint = [NSLayoutConstraint constraintWithItem:separator
                                                  attribute:NSLayoutAttributeWidth
                                                  relatedBy:NSLayoutRelationEqual
                                                     toItem:targetSeparator
                                                  attribute:NSLayoutAttributeWidth
                                                 multiplier:1.0
                                                   constant:0.0];
        [constraints addObject:constraint];
    }];
    
    return constraints;
}
+ (NSArray *)centerAlignmentConstraintsWithButtons:(NSArray *)buttons :(UIView *)Superview
{
    NSMutableArray *constraints = [[NSMutableArray alloc] init];
    
    NSMutableArray *buttonsAndSeparators = [[NSMutableArray alloc] init];
    
    [buttonsAndSeparators addObjectsFromArray:buttons];
    for (UIView *view in buttonsAndSeparators) {
        NSLayoutConstraint *constraint;
        constraint = [NSLayoutConstraint constraintWithItem:view
                                                  attribute:NSLayoutAttributeTop
                                                  relatedBy:NSLayoutRelationEqual
                                                     toItem:Superview
                                                  attribute:NSLayoutAttributeTop
                                                 multiplier:1.0
                                                   constant:0];
        [constraints addObject:constraint];
        constraint = [NSLayoutConstraint constraintWithItem:view
                                                  attribute:NSLayoutAttributeHeight
                                                  relatedBy:NSLayoutRelationEqual
                                                     toItem:Superview
                                                  attribute:NSLayoutAttributeHeight
                                                 multiplier:0
                                                   constant:CGRectGetHeight(Superview.bounds)];
        [constraints addObject:constraint];
    }
    
    return constraints;
}


+ (NSString *)encryptWithText:(NSString *)sText
{
    //kCCEncrypt 加密
    return [self encrypt:sText encryptOrDecrypt:kCCEncrypt key:@"19910925"];
}

+ (NSString *)decryptWithText:(NSString *)sText
{
    //kCCDecrypt 解密
    return [self encrypt:sText encryptOrDecrypt:kCCDecrypt key:@"19910925"];
}

+ (NSString *)encrypt:(NSString *)sText encryptOrDecrypt:(CCOperation)encryptOperation key:(NSString *)key
{
    const void *dataIn;
    size_t dataInLength;
    
    if (encryptOperation == kCCDecrypt)//传递过来的是decrypt 解码
    {
        //解码 base64
        NSData *decryptData =[[NSData alloc]initWithBase64EncodedString:sText options:0];
        //        NSData *decryptData = [GTMBase64 decodeData:[sText dataUsingEncoding:NSUTF8StringEncoding]];//转成utf-8并decode
        dataInLength = [decryptData length];
        dataIn = [decryptData bytes];
    }
    else  //encrypt
    {
        NSData* encryptData = [sText dataUsingEncoding:NSUTF8StringEncoding];
        dataInLength = [encryptData length];
        dataIn = (const void *)[encryptData bytes];
    }
    
    /*
     DES加密 ：用CCCrypt函数加密一下，然后用base64编码下，传过去
     DES解密 ：把收到的数据根据base64，decode一下，然后再用CCCrypt函数解密，得到原本的数据
     */
    CCCryptorStatus ccStatus;
    uint8_t *dataOut = NULL; //可以理解位type/typedef 的缩写（有效的维护了代码，比如：一个人用int，一个人用long。最好用typedef来定义）
    size_t dataOutAvailable = 0; //size_t  是操作符sizeof返回的结果类型
    size_t dataOutMoved = 0;
    
    dataOutAvailable = (dataInLength + kCCBlockSizeDES) & ~(kCCBlockSizeDES - 1);
    dataOut = malloc( dataOutAvailable * sizeof(uint8_t));
    memset((void *)dataOut, 0x0, dataOutAvailable);//将已开辟内存空间buffer的首 1 个字节的值设为值 0
    
    NSString *initIv = @"abcdefg";
    const void *vkey = (const void *) [key UTF8String];
    const void *iv = (const void *) [initIv UTF8String];
//    int har[] = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
    //CCCrypt函数 加密/解密
    ccStatus = CCCrypt(encryptOperation,//  加密/解密
                       kCCAlgorithmDES,//  加密根据哪个标准（des，3des，aes。。。。）
                       kCCOptionPKCS7Padding,//  选项分组密码算法(des:对每块分组加一次密  3DES：对每块分组加三个不同的密)
                       vkey,  //密钥    加密和解密的密钥必须一致
                       kCCKeySizeDES,//   DES 密钥的大小（kCCKeySizeDES=8）
                       iv, //  可选的初始矢量
                       dataIn, // 数据的存储单元
                       dataInLength,// 数据的大小
                       (void *)dataOut,// 用于返回数据
                       dataOutAvailable,
                       &dataOutMoved);
    
    NSString *result = nil;
    
    if (encryptOperation == kCCDecrypt)//encryptOperation==1  解码
    {
        //得到解密出来的data数据，改变为utf-8的字符串
        result = [[NSString alloc] initWithData:[NSData dataWithBytes:(const void *)dataOut length:(NSUInteger)dataOutMoved] encoding:NSUTF8StringEncoding];
    }
    else //encryptOperation==0  （加密过程中，把加好密的数据转成base64的）
    {
        //编码 base64
        NSData *data = [NSData dataWithBytes:(const void *)dataOut length:(NSUInteger)dataOutMoved];
        result = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    }
    
    return result;
}
+ (void)setTest:(NSString *)test
{ NSString *key = @"test";
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:test forKey:key];
    [defaults synchronize];

}
+ (NSString *)getTest
{
    NSString *key = @"test";
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *result =@"";
    if ([defaults objectForKey:key]) {
        result = [defaults objectForKey:key];
    }
    return result;

    
}
@end